<?php
namespace Admin\Controller;
use Admin\Controller\CommonController;
/**
 * 商品调拨控制器
 */
class AllotController extends CommonController{
	//调入明细
	public function allotin(){
		$pageNum=I('pageNum',1,intval);//第几页
		$numPerPage=I('numPerPage',10,intval);//每页显示条数
		//获取排序的字段及排序方式
		$orderField=$_POST['_order']?$_POST['_order']:'date';
		$orderDirection=$_POST['_sort']?$_POST['_sort']:'desc';
		//组合排序字符串
		$orders=($orderField.' '.$orderDirection);
      	$this->depotAll=M('depot')->where(array('del'=>0))->select();
		//加载检索的基础数据
		$this->search();
		//检索条件
        if($_POST['search_where']){
            $this->search_where=$_POST['search_where'];
        	$where=$this->_search();
        }
        //get方式查看明细
        if(!empty($_GET)){
        	if(isset($_GET['sequence'])){
        		$where['sequence']=array('eq',$_GET['sequence']);
        		$search_where['sequence']['like']=$_GET['sequence'];
        		$this->assign('search_where',$search_where);
        	}
        }
        //库房权限设置，没有权限的不显示
        if(empty($where['new_depot_id'])){
	        $depots=session('depots');
	        $where['new_depot_id']=array('in',$depots);
    	}
    	//在where条件中增加未删除的判断条件
       	$where['del']=array('eq','0');
       	unset($where['goods_name']);
		$db=D('AllotView');
		//获取总数据条数
		$count=$db->where($where)->count();
		$this->assign('totalCount',$count);//数据的总条数
		$this->assign('numPerPage',$numPerPage);//每页显示多少条
		$this->assign('pageNumShown',6);//页标数字多少个
		$this->assign('currentPage',$pageNum);//当前是第几页
		$page= new \Think\Page($count,$numPerPage);
		$this->list=$db->limit($numPerPage)->Page($pageNum)->order($orders)->where($where)->select();
		
		//权限菜单的显示与隐藏	
		$this->assign('allot_addId',66);//添加
		$this->assign('allot_refuseId',98);//拒绝调拨
		$this->assign('allot_allRefuseId',99);//批量拒绝调拨
		$this->assign('allot_receiveId',97);//接收调拨

		$this->display();
	}
	//调入日志
	public function recordIn(){
		$pageNum=I('pageNum',1,intval);//第几页
		$numPerPage=I('numPerPage',10,intval);//每页显示条数
		//获取排序的字段及排序方式
		$orderField=$_POST['_order']?$_POST['_order']:'date';
		$orderDirection=$_POST['_sort']?$_POST['_sort']:'desc';
		//组合排序字符串
		$orders=($orderField.' '.$orderDirection);
      	$this->depotAll=M('depot')->where(array('del'=>0))->select();
		//加载检索的基础数据
		$this->search();
		//检索条件
        if($_POST['search_where']){
            $this->search_where=$_POST['search_where'];
        	$where=$this->_search();
        }
        //库房权限设置，没有权限的不显示
        if(empty($where['new_depot_id'])){
	        $depots=session('depots');
	        $where['new_depot_id']=array('in',$depots);
    	}
    	//在where条件中增加未删除的判断条件
       	$where['del']=array('eq','0');
       	unset($where['goods_name']);
		$db=D('AllotView');
		//获取总数据条数
		$count=$db->where($where)->count();
		$this->assign('totalCount',$count);//数据的总条数
		$this->assign('numPerPage',$numPerPage);//每页显示多少条
		$this->assign('pageNumShown',6);//页标数字多少个
		$this->assign('currentPage',$pageNum);//当前是第几页
		$page= new \Think\Page($count,$numPerPage);
		$field=array('sequence','date','sender','receive','status','uid','depot_name','new_depot_name','sum(number) as sum','sum(number*price) as jine');
		$this->list=$db->limit($numPerPage)->Page($pageNum)->order($orders)->where($where)->field($field)->group('sequence')->select();
		$this->display();
	}
	//调出明细
	public function allotout(){
		$pageNum=I('pageNum',1,intval);//第几页
		$numPerPage=I('numPerPage',10,intval);//每页显示条数
		//获取排序的字段及排序方式
		$orderField=$_POST['_order']?$_POST['_order']:'date';
		$orderDirection=$_POST['_sort']?$_POST['_sort']:'desc';
		//组合排序字符串
		$orders=($orderField.' '.$orderDirection);
      	$this->depotAll=M('depot')->where(array('del'=>0))->select();
		//加载检索的基础数据
		$this->search();
		//检索条件
        if($_POST['search_where']){
            $this->search_where=$_POST['search_where'];
        	$where=$this->_search();
        }
        //get方式查看明细
        if(!empty($_GET)){
        	if(isset($_GET['sequence'])){
        		$where['sequence']=array('eq',$_GET['sequence']);
        		$search_where['sequence']['like']=$_GET['sequence'];
        		$this->assign('search_where',$search_where);
        	}
        }
        //库房权限设置，没有权限的不显示
        if(empty($where['depot_id'])){
	        $depots=session('depots');
	        $where['depot_id']=array('in',$depots);
    	}
    	//在where条件中增加未删除的判断条件
       	$where['del']=array('eq','0');
       	unset($where['goods_name']);
		$db=D('AllotView');
		//获取总数据条数
		$count=$db->where($where)->count();
		$this->assign('totalCount',$count);//数据的总条数
		$this->assign('numPerPage',$numPerPage);//每页显示多少条
		$this->assign('pageNumShown',6);//页标数字多少个
		$this->assign('currentPage',$pageNum);//当前是第几页
		$page= new \Think\Page($count,$numPerPage);
		$this->list=$db->limit($numPerPage)->Page($pageNum)->order($orders)->where($where)->select();
		//权限菜单的显示与隐藏	
		$this->assign('allot_addId',66);//添加
		$this->assign('allot_delId',101);//取消调拨
		$this->assign('allot_allDelId',100);//批量取消调拨
		
		$this->display();
	}

	//商品调拨操作界面及数据处理
	public function add(){
		if(!empty($_POST)){
			$arr=$_POST;		
			$receive=I('receive',string);
			if(!$receive) $this->error('请输入收货人姓名');
			$sender=I('sender',string);
			if(!$sender) $this->error('请输入发料人姓名');
			$depot_id=I('depot_id',string);
			if(!$depot_id) $this->error('请选择调入库房');
			$depot_name=I('depot_name',string);
			foreach ($arr['goods_number'] as $v) {
				if ($v==0) $this->error('产品数量不能为0');
			}
			$depot_name_temp=$arr['goods_depot_name'][0];
			foreach ($arr['goods_depot_name'] as $v) {
				if(!$depot_name_temp==$v) $this->error('调出物料的库房必须一致');
				if ($v==$depot_name) $this->error('调入库房与调出库房不能一致');
			}
			//p($arr);die;
			//调拨商品的总数量
			$count=count($arr['goods_id']);		
			$t=time();
			$date=strtotime($arr['date']);
			$db=M('allot');
			$sql="SELECT nextval('SEQ_DB') seq";
			$seq=($db->query($sql));
			$seq='DB'.$seq[0]['seq'];
			for ($i=0; $i <$count ; $i++) { 
				$code=$arr['goods_id'][$i];
				$pay=$arr['goods_number'][$i];
				$price=$arr['goods_price'][$i];
				$data=array(
					'sequence'=>$seq,
					'new_depot_id'=>$depot_id,
					'old_code'=>$code,
					'date'=>$date,
					'number'=>$pay,
					'status'=>'1',
					'createtime'=>$t,
					'receive'=>$receive,
					'sender'=>$sender,
					'uid'=>session('uid')
				);
				if($db->add($data)){

				}else{
					$this->error('调拨失败');
				}
			}
			$this->success('调拨成功');
		}else{
			$this->display();
		}
	}
		//调出日志
	public function recordOut(){
		$pageNum=I('pageNum',1,intval);//第几页
		$numPerPage=I('numPerPage',10,intval);//每页显示条数
		//获取排序的字段及排序方式
		$orderField=$_POST['_order']?$_POST['_order']:'date';
		$orderDirection=$_POST['_sort']?$_POST['_sort']:'desc';
		//组合排序字符串
		$orders=($orderField.' '.$orderDirection);
      	$this->depotAll=M('depot')->where(array('del'=>0))->select();
		//加载检索的基础数据
		$this->search();
		//检索条件
        if($_POST['search_where']){
            $this->search_where=$_POST['search_where'];
        	$where=$this->_search();
        }
        //库房权限设置，没有权限的不显示
        if(empty($where['depot_id'])){
	        $depots=session('depots');
	        $where['depot_id']=array('in',$depots);
    	}
    	//在where条件中增加未删除的判断条件
       	$where['del']=array('eq','0');
       	unset($where['goods_name']);
		$db=D('AllotView');
		//获取总数据条数
		$count=$db->where($where)->count();
		$this->assign('totalCount',$count);//数据的总条数
		$this->assign('numPerPage',$numPerPage);//每页显示多少条
		$this->assign('pageNumShown',6);//页标数字多少个
		$this->assign('currentPage',$pageNum);//当前是第几页
		$page= new \Think\Page($count,$numPerPage);
		$field=array('sequence','date','sender','receive','status','uid','depot_name','new_depot_name','sum(number) as sum','sum(number*price) as jine');
		$this->list=$db->limit($numPerPage)->Page($pageNum)->order($orders)->where($where)->field($field)->group('sequence')->select();
		$this->display();
	}

	//接受调拨入库，将调拨记录由正常状态改为接收状态
	public function receive(){
		$id = $_GET['id'];
		if(isset($id)){
			//判断用户对调入仓库的权限
			$RM=M('allot')->where(array('new_depot_id'=>array('in',session('depots')),'id'=>$id))->find();
			if(!$RM) $this->error('操作非法');
			$db=D('AllotView');
			$field=array('id','goods_id','supplier_id','new_depot_id','code','number','price');
			$result=$db->field($field)->find($id);
			$codeSQL="SELECT nextval('SEQ_CODE') code";
			$code=($db->query($codeSQL));
			$code='TXM'.$code[0]['code'];
			if(M('allot')->where(array('id'=>$result['id']))->setField('new_code',$code)){
				$data=array(
					'code'=>$code,
					'goods_id'=>$result['goods_id'],
					'depot_id'=>$result['new_depot_id'],
					'supplier_id'=>$result['supplier_id'],
					'price'=>$result['price'],
					'createtime'=>time(),
					'edittime'=>time(),
					'uid'=>session('uid')
					);
				if(M('code')->add($data)){
					$data2=array(
						'code_code'=>$code,
						'movein'=>$result['number']
						);
					if(M('number')->add($data2)){						
						if(M('number')->where(array('code_code'=>$result['code']))->setInc('moveout',$result['number'])){
							M('allot')->where(array('id'=>$result['id']))->save(array('type'=>2,'status'=>2,'uid'=>session('uid')));
							$this->success('接收成功');
						}else{
							$this->error ('原库房商品数量移动失败');
						}
					}else{
						$this->error ('新库房商品数量添加失败');
					}

				}else{
					$this->error ('新编码添加失败');
				}
			}else{
				$this->error ('调拨记录中新编码写入失败');
			}

		}

	}

	//拒绝调拨入库，将调拨记录由正常状态改为删除状态
	public function refuse(){
		$id = $_GET['id'];
		if(isset($id)){
			if (M(CONTROLLER_NAME)->where(array('id'=>$id))->setField('type',0)) {
                    $this->success ('成功拒绝');
                } else {
                    $this->error ('拒绝接受失败');
            }
		}
	}
	//批量拒绝调拨入库，将调拨记录由正常状态改为删除状态
	public function allRefuse(){
		if(!empty($_POST)){
			$ids=$_POST['ids'];
			if(isset($ids)){
				if(M(CONTROLLER_NAME)->where(array('id'=>array('in',$ids)))->setField('type',0)){
					$this->success ('成功拒绝');
                } else {
                    $this->error ('拒绝接受失败');
            	}

			}
		}
	}
	//批量拒绝调拨入库，将调拨记录由正常状态改为删除状态
	public function allDelete(){
		if(!empty($_POST)){
			$ids=$_POST['ids'];
			if(isset($ids)){
				if(M(CONTROLLER_NAME)->where(array('id'=>array('in',$ids)))->setField('del',1)){
					$this->success ('成功拒绝');
                } else {
                    $this->error ('拒绝接受失败');
            	}

			}
		}
	}
}